﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using fqlmoney.Model;
using MySql.Data.MySqlClient;

namespace fqlmoney.DAL
{
    public class UserInfoDAL
    {
        /// <summary>
        /// 根据登录名称获取用户
        /// </summary>
        /// <param name="loginname"></param>
        /// <returns></returns>
        public userinfo GetUserInfoByLoginName(string loginname)
        {
            return DBUtility.MySqlHelper.ExecuteDataReaderModel<userinfo>("SELECT userinfo.*,`qq_openid`,`weibo_openid`,`weixin_openid` FROM `userinfo` LEFT JOIN `user_oauth` ON userinfo.`u_id`=user_oauth.`u_id` WHERE `u_loginname`=@loginname", new MySqlParameter("@loginname", loginname));
        }

        /// <summary>
        /// 根据qqopenid获取用户信息
        /// </summary>
        /// <param name="openid"></param>
        /// <returns></returns>
        public userinfo GetModelByQQOpenid(string openid)
        {
            return DBUtility.MySqlHelper.ExecuteDataReaderModel<userinfo>("SELECT userinfo.*,`qq_openid`,`weibo_openid`,`weixin_openid` FROM `userinfo` LEFT JOIN `user_oauth` ON userinfo.`u_id`=user_oauth.`u_id` WHERE `qq_openid`=@openid",new MySqlParameter("@openid", openid));
        }

        /// <summary>
        /// 根据WeiBoopenid获取用户信息
        /// </summary>
        /// <param name="openid"></param>
        /// <returns></returns>
        public userinfo GetModelByWeiBoOpenid(string openid)
        {
            return DBUtility.MySqlHelper.ExecuteDataReaderModel<userinfo>("SELECT userinfo.*,`qq_openid`,`weibo_openid`,`weixin_openid` FROM `userinfo` LEFT JOIN `user_oauth` ON userinfo.`u_id`=user_oauth.`u_id` WHERE `weibo_openid`=@openid", new MySqlParameter("@openid", openid));
        }

        /// <summary>
        /// 根据WeiXinopenid获取用户信息
        /// </summary>
        /// <param name="openid"></param>
        /// <returns></returns>
        public userinfo GetModelByWeiXinOpenid(string openid)
        {
            return DBUtility.MySqlHelper.ExecuteDataReaderModel<userinfo>("SELECT userinfo.*,`qq_openid`,`weibo_openid`,`weixin_openid` FROM `userinfo` LEFT JOIN `user_oauth` ON userinfo.`u_id`=user_oauth.`u_id` WHERE `weixin_openid`=@openid", new MySqlParameter("@openid", openid));
        }

        /// <summary>
        /// 根据id获取用户信息
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public userinfo GetModelByID(string uid)
        {
            return DBUtility.MySqlHelper.ExecuteDataReaderModel<userinfo>("SELECT userinfo.*,`qq_openid`,`weibo_openid`,`weixin_openid` FROM `userinfo` LEFT JOIN `user_oauth` ON userinfo.`u_id`=user_oauth.`u_id` WHERE `u_id`=@uid", new MySqlParameter("@uid",uid));
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddUser(userinfo model)
        {
            MySqlParameter[] para = {
                new MySqlParameter("@u_name",model.u_name),
                new MySqlParameter("@u_headimg",model.u_headimg),
                new MySqlParameter("@qq_openid",model.qq_openid),
                 new MySqlParameter("@weibo_openid",model.weibo_openid),
                  new MySqlParameter("@weixin_openid",model.weixin_openid)
            };
            return DBUtility.MySqlHelper.ExecuteNonQuery("CALL AddUser(@u_name,@u_headimg,@qq_openid,@weibo_openid,@weixin_openid)", para);
        }

        


    }
}
